package cn.com.dhcc.app.pub.core.listener;

import java.io.IOException;
import java.rmi.server.RMISocketFactory;

import javax.servlet.ServletContextEvent;

import cn.com.dhcc.app.core.AppEnv.ParaGetTool;
import cn.com.dhcc.app.core.boot.BootParam;
import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.handler.remotehandler.rmi.SMRMISocket;
import cn.com.dhcc.app.core.listener.AppInitListener;
import cn.com.dhcc.app.pub.core.consts.AppConst;

/**
 * spring 容器启动之前的一些操作
 */
public class BeforeSpringInitListener implements AppInitListener {
	public static int  rmiDataConnPort = ParaGetTool.getPara(Integer.class, "app.rmi.data_conn_port", 0);//rmi数据传输端口
	@Override
	public void init(ServletContextEvent contextEvent, BootParam bootParam) throws InitException {
		//rmi host的设置必须在rmi上注册的服务对象创建之前启动
		System.setProperty("java.rmi.server.hostname", AppConst.installIp);
		if(rmiDataConnPort > 0){
			try {
				RMISocketFactory.setSocketFactory(new SMRMISocket(rmiDataConnPort));// 定义数据传输端口
			} catch (IOException e) {
				throw new RuntimeException("定义数据传输端口时抛出异常，"+e);
			} 
		}
	}

	@Override
	public void destroyed(ServletContextEvent contextEvent, BootParam bootParam) throws Exception {
	}
}
